// To change this License template, choose Tools / Templates
// and edit Licenses / FanDefaultLicense.txt
//
// History:
//   07.09.2010 JSTAUDEMEYER Creation
//

**
** Tune
**
class Tune : MusicElement
{
  internal Note[] notes := [,]


  new make(Note? n:=null) {
    //echo("Tune.make $n")
    if (n!=null) addNote(n)
  }

  new fromList(Note[] list) {
    notes = list
  }

  @Operator
  Note get(Int n) { notes[n] }

  Void addNote(Note n) {
    notes.add(n)
  }

  Void addTune(Tune t) {
    notes.addAll(t.notes)
  }

  Tune map (|Note->Note| f) {
    Tune.fromList(notes.map(f))
  }

  @Operator
  override Tune plus(MusicElement e) {
    if (e is Note) {
      addNote(e)
    } else if (e is Tune) {
      addTune(e)
    } else {
      throw Err("Unknown music element: "+e)
    }
    /*
    switch (e) {
      case Note: addNote(e)
      case Tune: addTune(e)
    }
    */
    return this
  }

  @Operator
  override This mult (Num n) { map { it.mult(n) } }

  @Operator
  override This div (Num n) { map { it.div(n) } }

  override Obj? trap(Str name, Obj?[]? args) {
    notes.trap(name,args)
  }

  override Float length() { notes.reduce(0f) |r,v| { v.length + (Float)r } }

  override Int size() { notes.size }

  override Str toStr() { "("+notes.join("+")+")" }

}